package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.executor.MyExecutor;
import com.atguigu.result.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

@Controller
public class WebSkuDetailController {

    @Autowired
    ProductFeignClient productFeignClient;
    @Autowired
    SearchFeignClient searchFeignClient;
    //编写访问的控制器！
    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){
        HashMap<String, Object> map = new HashMap<>();


        //a.根据skuId获取商品sku的基本信息//有返回值
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        });

        //b.根据三级分类id获取sku的分类信息
        CompletableFuture<Void> categoryViewFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            Long category3Id = skuInfo.getCategory3Id();
            BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
            map.put("categoryView", categoryView);
        }, MyExecutor.getInstance());

        //没有返回值
        //c.根据skuId获取sku的实时价格
        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        }, MyExecutor.getInstance());

        //d.根据skuId获取所有的spu销售属性与该sku所勾选的销售属性
        CompletableFuture<Void> spuSalePropertyListFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            Long productId = skuInfo.getProductId();
            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSalePropertyAndSelected(productId, skuId);
            map.put("spuSalePropertyList", spuSalePropertyList);
        }, MyExecutor.getInstance());


        //e.查询销售属性组合所对于skuId的对于关系
        CompletableFuture<Void> salePropertyValueIdJsonFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            Map salePropertyAndSkuIdMap = productFeignClient.getSalePropertyAndSkuIdMapping(skuInfo.getProductId());
            String s = JSON.toJSONString(salePropertyAndSkuIdMap);
            map.put("salePropertyValueIdJson", s);
        }, MyExecutor.getInstance());


        //当点击商品详情的时候，热度加一
        CompletableFuture<Void> incrHotScoreFuture = CompletableFuture.runAsync(()->{
            searchFeignClient.incrHotScore(skuId);
        },MyExecutor.getInstance());

        //多任务组合,必须五个任务全部执行完拿到数据，才能赋值给页面
        CompletableFuture.allOf(
                skuInfoFuture,
                categoryViewFuture,
                priceFuture,
                spuSalePropertyListFuture,//一定需要添加join，join是联合起来，执行全部完成
                salePropertyValueIdJsonFuture,
                incrHotScoreFuture).join();



        //注意写成addAllAttributes，不要写成addAttributes
        model.addAllAttributes(map);

        return "detail/index";
    }
}
